<?php
/*
 *   This file is part of verbum.
 *
 *   poat is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   poat is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with poat.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * 	 Copyright (c) 2008 Sergio Gabriel Teves <gabriel.sgt at gmail.com>
 */

if ($op=="save") {
	$ids = json_decode(stripslashes($param['ids']));
	$values = json_decode($param['values']);

	for($i=0;$i<count($ids);$i++) {
		$id = $ids[$i];
		$value = $values[$i];

		$l = new i18n;
		$l->id = $id;
		$r = $loader->get($l);
		if ($r) {
			if (trim($value)!="") {
				$l->translated = $value;
				$l->status = LANG_OK;
				$loader->update($l);
				cache(langCacheKey($l->original,$l->lang),$l->translated);
			} else {
				$_log->debug("Ommit: $id");
			}
		}
	}
	success();
} else {
	if ($CURRENT_USER->lang!=DEFAULT_LANG) {
		$lng = $CURRENT_USER->lang;	
	} else {
		$t = new team;
		$t->id = getTeamId($CURRENT_USER);
		$r = $loader->get($t);
		if ($r) {
			$lng = strtolower($t->lang);
		}		
	}
	
	if (isset($lng)) {
		$l = new i18n;
		$l->lang = $lng;
		$list = $loader->loadListBy($l,$l->listBy(true));
		if (count($list)==0) {
			if ($loader->execute($l->fillUpLang($lng))>0) {
				$list = $loader->loadListBy($l,$l->listBy(true));		
			}
		}
		out(jsonList($list,array("id","#original","#translated","#previous","status")));		
	} else {
		failed("INVALID");
	}

}
?>